package com.cvte.testui.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cvte.testui.R;
import com.cvte.vdtlibrary.bluetooth.EntityBluetoothDevice;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.logging.LogRecord;

/**
 * Created by user on 2018/12/18.
 */
public class BtDevAdapter extends BaseAdapter {

    private List<EntityBluetoothDevice> list;
    private Context context;


    public BtDevAdapter(Context context,List<EntityBluetoothDevice> list) {
        this.list = new ArrayList<EntityBluetoothDevice>();
        this.list.addAll(list);
        this.context = context;
    }

    public void setList(List<EntityBluetoothDevice> devList){
        this.list.clear();
        this.list.addAll(devList);
        Log.d("wgt","setList  list size() = " + devList.size());
        for(int i = 0;i < list.size(); i++) {
            Log.d("wgt", " = " + list.get(i).getMAC() + list.get(i).getName());
        }
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.d("wgt","getView  = " + position);
        ViewHolder holder = null;
        if(convertView == null){
            holder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(R.layout.bt_dev_item,null,true);
            holder.mac = (TextView)convertView.findViewById(R.id.bt_dev_mac);
            holder.name = (TextView)convertView.findViewById(R.id.bt_dev_name);
            holder.status = (TextView)convertView.findViewById(R.id.bt_dev_status);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder)convertView.getTag();
        }
        holder.mac.setText(list.get(position).getMAC());
        holder.name.setText(list.get(position).getName());
        holder.status.setText(list.get(position).getStatus().toString());
        return convertView;
    }
    class ViewHolder{
        TextView name;
        TextView mac;
        TextView status;
    }
}
